import 'package:flutter/material.dart';
import 'package:my_flutter/pages/home_page.dart';
import 'package:my_flutter/pages/user_page.dart';
import 'package:my_flutter/plugins/mix_plugin.dart';

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: IndexPage(),
    );
  }
}

class IndexPage extends StatefulWidget {
  const IndexPage({Key? key}) : super(key: key);

  @override
  State<IndexPage> createState() => _IndexPageState();
}

class _IndexPageState extends State<IndexPage> {
  var pageCtrl = PageController();
  @override
  void initState() {
    super.initState();
    print("初始化成功");
    FlutterMixPlugin.addListener((p0) {
      print("插件发过来的值时：${p0}");
      if (p0 == "home") {
        pageCtrl.jumpToPage(0);
      }
      if (p0 == "user") {
        pageCtrl.jumpToPage(1);
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return PageView.builder(controller: pageCtrl,itemBuilder: (ctx,index){
      if (index == 0) {
        return HomePage();
      }
      if (index == 1) {
        return UserPage();
      }
      return Container();
    },physics: const NeverScrollableScrollPhysics());
  }
}

